#define redColor        		vec4(1.0, 0.0, 0.0, 1.0)
#define greenColor        		vec4(0.0, 1.0, 0.0, 1.0)
#define blueColor        		vec4(0.0, 0.0, 1.0, 1.0)
#define whiteColor        		vec4(1.0, 1.0, 1.0, 1.0)

#define getLuminance(p) 									dot(p.xyz, vec3(0.2126, 0.7152, 0.0722))
#define getRelativeSample(x,y)  							unpremultiply(sample(image, samplerTransform(image, dest + vec2(x,y))))
#define getSample(x,y)  									unpremultiply(sample(image, samplerTransform(image, vec2(x,y))))
#define difference(a,b) 									length(a - b)
#define incrementForSimilarity(a, b) 						compare(threshold - difference(a,b), 0.0, increment)

const vec3 clearColor	    = vec3(0);
const float threshold 		= 0.0125;
const float increment 		= 0.25;
const float endcapDivisor 	= 32.0;
const vec3 upscaleTeal      = vec3(0.514, 0.933, 0.933);

kernel vec4 focus(sampler image) {
    vec2 dest = destCoord();

    float x 			= dest.x;
    float height 		= samplerExtent(image).w;
    float width 		= samplerExtent(image).z;
    float targetGray	= dest.y/height;

	float accumulator = 0.0;
	for(int y = 0; y < int(height); y++) {

		//Luminance
		accumulator += incrementForSimilarity(targetGray, getLuminance(getSample(x,y)));

	}

	//Calculate alpha 
	float endCap = (width/endcapDivisor);
	float alpha = min(1.0, ((difference(x, width)/endCap))) * (min(1.0, ((difference(x, 0.0)/endCap))) * accumulator);

	//Make accumulator 1 or 0
	accumulator = step(0.0,accumulator);


	// upscaleTeal
    return premultiply(vec4(mix(clearColor, upscaleTeal, accumulator),alpha));
}
